#include "not.hpp"

void		Not::solve()
{
  if (this->_state == UNDEFINED)
    {
      bool					gotTrue = false;
      bool					gotFalse = false;
      unsigned int				nb_entries = 0;
      AObject* elem;

      for (unsigned int i = 0; i < this->_cond.size(); ++i)
	{
	  ++nb_entries;
	  elem = this->_cond.front();
	  this->_cond.pop();
	  elem->solve();
	  if (elem->getState() == FALSE)
	    {
	      gotFalse = true;
	      this->_state = TRUE;
	    }
	  if (elem->getState() == TRUE)
	    {
	      gotTrue = true;
	      this->_state = FALSE;
	    }
	  if (elem->getState() == UNDEFINED)
	    {
	      this->_state = UNDEFINED;
	      this->_cond.push(elem);
	      return ;
	    }
	  this->_cond.push(elem);
	}
      if ((gotTrue && gotFalse) || nb_entries == 0)
	this->_state = UNDEFINED;
    }
}

bool		Not::behaviour(LinkChain lk, LinkChainIterator lk_it, LinkChainIterator, LinkChainIterator next)
{
  if (next != lk->end())
    {/*
      if (((next->second).first)->getType() == this->getType())
	{
	  std::cout << "Syntax error ";
	  return false;
	  }*/
      ((lk_it->second).first)->addObject((next->second).first);
      ((next->second).first)->getRequiredLinks(lk, next, lk_it);
      return true;
    }
  return false;
}
